package com.city.justlisten.controller;


import com.city.justlisten.entity.Rank;
import com.city.justlisten.service.RankService;
import com.city.justlisten.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class RankController {

    @Autowired
    private RankService rankService;

    /**
     * 新增评价
     */
    @PostMapping("/rank/add")
    public Result add(HttpServletRequest request){
        String songListId = request.getParameter("songListId");
        String consumerId = request.getParameter("consumerId");
        String score = request.getParameter("score");

        Rank rank = new Rank();
        rank.setSongListId(Integer.parseInt(songListId));
        rank.setConsumerId(Integer.parseInt(consumerId));
        rank.setScore(Integer.parseInt(score));
        boolean flag = rankService.insert(rank);
        if(flag){
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 计算平均分
     */
    @GetMapping("/rank")
    public Result rankOfSongListId(HttpServletRequest request){
        String songListId = request.getParameter("songListId");
        int i = rankService.rankOfSongListId(Integer.parseInt(songListId));
        return Result.ok().data("sum",i);
    }
}